package com.ohuang.jiaxiaotong.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import com.ohuang.jiaxiaotong.R;
public class CourseTableRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private ArrayList<String> mData;

    public CourseTableRvAdapter(Context mContext, ArrayList<String> mData) {
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mData = mData;
    }

    public void setMData(ArrayList<String> mData){
        this.mData=mData;
    }

    public void setDataForPosition(int position,String s){
        mData.set(position,s);
        notifyItemChanged(position);
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return new CourseTableRvAdapter.MyTVHolder(mLayoutInflater.inflate(R.layout.rv_txt_item, parent, false));
        } else {
           return new CourseTableRvAdapter.MyTVHolder2(mLayoutInflater.inflate(R.layout.rv_txt2_item, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof CourseTableRvAdapter.MyTVHolder) {
            ((MyTVHolder) holder).mTextView.setText(mData.get(position));

        }else if (holder instanceof  CourseTableRvAdapter.MyTVHolder2){
            if (onItemClickListener!=null) {
            ((MyTVHolder2) holder).checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.CheckBoxClick(((MyTVHolder2) holder).checkBox,pos);
                }
            });
            }
        }
        if (onItemClickListener!=null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder, pos);
                }
            });
        }

    }

    public class MyTVHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        MyTVHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.tv_txt);
        }
    }

    class MyTVHolder2 extends RecyclerView.ViewHolder {
        CheckBox checkBox;
        MyTVHolder2(View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.tv_check2);

        }
    }

    private CourseTableRvAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(CourseTableRvAdapter.OnItemClickListener mOnItemClickListener) {
        this.onItemClickListener = mOnItemClickListener;
    }

    //监听接口
    public interface OnItemClickListener {
        void onItemClick(RecyclerView.ViewHolder holder, int position);
        void CheckBoxClick(View view, int position);

    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public int getItemViewType(int position) {
        if (mData.get(position).isEmpty()) {
            return 0;
        } else {
            return 1;
        }
    }
}
